×
☰ See All Chapters

Spring @PropertySource Annotation

Spring @PropertySource annotation can be used to read from properties file. This annotation can be used along with @Configuration annotation.

In previous chapter we read to inject the default values to fields and parameters of constructor/methods of spring managed beans. In this tutorial we learn to inject the values from properties files.

Spring @Value annotation can be used to specify expression on field or methods and can also set default value and configure to read from properties file. If properties file is not found or specified property is not found then spring uses default value.  When you are injecting values from properties files, we specify placeholder in @Value annotation. Placeholders represent the property from properties file. When you use place holders, you should create PropertySourcesPlaceHolderConfigurer bean. Though we don’t use this bean explicitly, spring context should have this bean. Spring uses this bean internally.

Syntax to specify property in @Value annotation

Without default value

@Value("${propertyName}")

 

Value of the property with name propertyName will be injected. If property not found then spring throws exception.

With default value

@Value("${propertyName:defaultValue}")

 

Value of the property with name propertyName will be injected. If property not found then defaultValue will be injected.

Syntax to specify map property in @Value annotation

We can use SpEL to inject map value from properties file, considering the below property added in properties file, below table lists out the syntax to inject values from this property.

employeeSkills={1: 'JAVA', 2: 'Servlet', 3: 'JSP'}

Injecting complete map, Without default value

@Value("#{${employeeSkills}}")

private Map<Integer, String> skillsMap;       

Injecting complete map, With default value

@Value("#{${mainSkills: {100: 'JAVA'}}}")

private Map<Integer, String> mainSkillMap;

       

Spring @PropertySource Annotation Example

Let us see an example for how we can use @Value annotation to inject default value.   This example also shows how we can inject value to map from properties file.

employee.properties

employeeName=Manu Manjunatha

employeeSkills={100: 'JAVA', 102: 'Servlet', 103: 'JSP'}

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring3_PropertySource</artifactId>

        <version>0.0.1-SNAPSHOT</version>

 

        <dependencies>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-aspects</artifactId>

                        <version>${spring.version}</version>

                </dependency>

        </dependencies>

 

        <properties>

                <spring.version>4.2.4.RELEASE</spring.version>

        </properties>

 

</project>

Employee.java

package com.java4coding;

 

import java.util.Map;

 

public class Employee {

 

        private String employeeName;

 

        private String employeeId;

 

        private Map<Integer, String> allSkillsMap;

 

        private Map<Integer, String> mainSkillMap;

 

        public String getEmployeeName() {

                return employeeName;

        }

 

        public void setEmployeeName(String employeeName) {

                this.employeeName = employeeName;

        }

 

        public String getEmployeeId() {

                return employeeId;

        }

 

        public void setEmployeeId(String employeeId) {

                this.employeeId = employeeId;

        }

 

        public Map<Integer, String> getAllSkillsMap() {

                return allSkillsMap;

        }

 

        public void setAllSkillsMap(Map<Integer, String> allSkillsMap) {

                this.allSkillsMap = allSkillsMap;

        }

 

        public Map<Integer, String> getMainSkillMap() {

                return mainSkillMap;

        }

 

        public void setMainSkillMap(Map<Integer, String> mainSkillMap) {

                this.mainSkillMap = mainSkillMap;

        }

}

SpringConfiguration.java

package com.java4coding;

 

import java.util.Map;

 

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

 

@Configuration

@PropertySource("classpath:employee.properties")

public class SpringConfiguration {

 

        @Value("${employeeName}")

        private String employeeName;

 

        @Value("${employeeId:1}")

        private String employeeId;

 

        @Value("#{${employeeSkills}}")

        private Map<Integer, String> skillsMap;

 

        @Value("#{${mainSkills: {100: 'JAVA'}}}")

        private Map<Integer, String> mainSkillMap;

 

        // PropertySourcesPlaceHolderConfigurer Bean is required to support place holders in @Value

        @Bean

        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {

                return new PropertySourcesPlaceholderConfigurer();

        }

 

        @Bean

        public Employee getEmployee() {

                Employee employee = new Employee();

                employee.setEmployeeName(employeeName);

                employee.setEmployeeId(employeeId);

                employee.setAllSkillsMap(skillsMap);

                employee.setMainSkillMap(mainSkillMap);

                return employee;

        }

 

}

Demo.java

package com.java4coding;

 

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

public class Demo {

        public static void main(String[] args) {

                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class);

                Employee employee = (Employee) ctx.getBean("getEmployee");

                System.out.println(employee.getEmployeeName());

                System.out.println(employee.getEmployeeId());

                System.out.println("All Skills: " + employee.getAllSkillsMap());

                System.out.println("Main Skills: " + employee.getMainSkillMap());

        }

}

Output:

spring-propertysource-annotation-0
 

All Chapters
Author